package org.hackystat.devcathlon.ui.page.scoreboard.gameinfopanel;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.imageio.ImageIO;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.image.resource.BufferedDynamicImageResource;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.hackystat.devcathlon.engine.match.Match;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.ui.DevcathlonSession;
import org.hackystat.devcathlon.ui.page.DevcathlonBasePage;
import org.hackystat.devcathlon.ui.page.matches.MatchesSession;
import org.hackystat.devcathlon.ui.page.scoreboard.commentpanel.CommentPanel;
import org.hackystat.devcathlon.ui.page.scoreboard.eventspanel.EventsPanel;
import org.hackystat.devcathlon.ui.page.scoreboard.performerpanel.PerformerPanel;
import org.hackystat.devcathlon.ui.page.scoreboard.scorespanel.ScoresPanel;
import org.hackystat.devcathlon.ui.page.scoreboard.teamleftpanel.TeamLeftPanel;
import org.hackystat.devcathlon.ui.page.scoreboard.teamrightpanel.TeamRightPanel;

/**
 * Connects a match to the information panel.
 * 
 * @author Daniel Arakaki
 */
public class GameInfoPage extends DevcathlonBasePage {
  
  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  
  /**
   * Sets up a page for the game information panel.
   * 
   * @param match A clicked match from the Scores panel.
   */
  public GameInfoPage(Match match) {
    
    add(new WebMarkupContainer("body") {
      /** Support serialization. */
      private static final long serialVersionUID = 1L;
      
      @Override
      public boolean isTransparentResolver() {
        return true;
      }
      
      @Override
      protected void onComponentTag(ComponentTag tag) {
        tag.put("class", "thrColElsHdr section-4");
      }
    });
    
    get("ScoreboardLink").add(new SimpleAttributeModifier("class", "selected"));
    
    DevcathlonSession devcathlonSession = DevcathlonSession.get();
    MatchesSession matchesSession = devcathlonSession.getMatchesSession();
    
    add(new ScoresPanel("scores_panel", matchesSession.getMatchNames()));
    add(new GameInfoPanel("gameinfo_panel", matchesSession.getMatch(match.getName())));
    add(new TeamLeftPanel("teamleft_panel", matchesSession.getMatch(match.getName()).getName()));
    add(new TeamRightPanel("teamright_panel", matchesSession.getMatch(match.getName()).getName()));
    add(new EventsPanel("events_panel", matchesSession.getMatch(match.getName())));
    add(new CommentPanel("comments_panel", matchesSession.getMatch(match.getName())));
    add(new PerformerPanel("performer_panel", matchesSession.getMatch(match.getName()).getName()));
    
    //Insert team logos
    Set<Team> teams = matchesSession.getMatch(match.getName()).getTeams();
    List<Team> teamList = new ArrayList<Team>(teams);
    
    ListView listView = new ListView("TeamLogos", teamList) {

      /** Support serialization. */
      private static final long serialVersionUID = 1L;

      @Override
      protected void populateItem(ListItem logoItem) {
        Team teamLogo = (Team) logoItem.getModelObject();
        
        try {
          BufferedDynamicImageResource resource = new BufferedDynamicImageResource();     
          BufferedImage image = ImageIO.read(new File(teamLogo.getTeamLogoPath()));
          resource.setImage(image);
          logoItem.add(new Image("TeamLogo", resource)); 
        }
        catch (IOException e) {
          logoItem.add(new Image("TeamLogo"));
        }
      }
      
    };
    add(listView);
  }
}
